<template>
  <div class="auto-paper">
    <h3>为考试 {{ examCode }} 配置组卷规则</h3>

    <el-form label-width="120px">
      <el-form-item label="考试科目">
        <el-input v-model="paperConfig.subject" disabled />
      </el-form-item>

      <el-form-item label="单选题数量">
        <el-input-number v-model="paperConfig.multipleCount" :min="0" :max="100" />
        <el-input v-model.number="paperConfig.multipleScore" type="number" size="small" style="width: 100px" /> 分/题
      </el-form-item>

      <el-form-item label="多选题数量">
        <el-input-number v-model="paperConfig.fillCount" :min="0" :max="100" />
        <el-input v-model.number="paperConfig.fillScore" type="number" size="small" style="width: 100px" /> 分/题
      </el-form-item>

      <el-form-item label="判断题数量">
        <el-input-number v-model="paperConfig.judgeCount" :min="0" :max="100" />
        <el-input v-model.number="paperConfig.judgeScore" type="number" size="small" style="width: 100px" /> 分/题
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="saveConfig">保存配置</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      examCode: this.$route.query.examCode,
      paperId: this.$route.query.paperId,
      paperConfig: {
        subject: this.$route.query.subject,
        bankSubject: this.$route.query.bankSubject,
        fillCount: 20,
        fillScore: 2.0,
        multipleCount: 10,
        multipleScore: 3.0,
        judgeCount: 10,
        judgeScore: 1.0
      }
    };
  },
  methods: {
    // ✅ 只保存配置，不拉题
    async saveConfig() {
      try {
        const config = {
          paperId: this.paperId,
          subject: this.paperConfig.subject,
          bankSubject: this.paperConfig.bankSubject,
          multipleCount: this.paperConfig.multipleCount,
          multipleScore: this.paperConfig.multipleScore,
          fillCount: this.paperConfig.fillCount,
          fillScore: this.paperConfig.fillScore,
          judgeCount: this.paperConfig.judgeCount,
          judgeScore: this.paperConfig.judgeScore
        };

        const res = await this.$axios.post('/api/paperConfig/save', config);
        if (res.data.code === 200) {
          this.$message.success('组卷规则已保存，学生考试时将随机出题');
          this.$router.push({ path: '/selectExam' });
        } else {
          this.$message.error(res.data.message || '保存失败');
        }
      } catch (error) {
        console.error('保存失败:', error);
        this.$message.error('网络错误，请重试');
      }
    },
    cancel() {
      this.$router.back();
    }
  }
};
</script>